<template>
	<a-drawer
		:title="formData.id ? '编辑房间' : '增加房间'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="区域：" name="areaId">
						<a-select v-model:value="formData.areaId" placeholder="请选择区域" :options="areaOptions" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="楼宇：" name="buildingId">
						<a-select
							v-model:value="formData.buildingId"
							placeholder="请选择楼宇"
							:options="buildingOptions"
							allow-clear
						/>
					</a-form-item>
				</a-col>
			</a-row>

			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="部门：" name="orgId">
						<a-tree-select
							v-model:value="formData.orgId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择所属组织"
							allow-clear
							tree-default-expand-all
							:tree-data="treeData"
							:field-names="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
							selectable="false"
							tree-line
						></a-tree-select>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="门牌号：" name="code">
						<a-input v-model:value.trim="formData.code" placeholder="请输入门牌号" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-form-item label="房间名称：" name="name">
				<a-input v-model:value.trim="formData.name" placeholder="请输入房间名称" allow-clear />
			</a-form-item>

			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="负责人：" name="inChargeUserName">
						<a-input
							v-model:value="formData.inChargeUserName"
							placeholder="请输入负责人"
							allow-clear
							@click="inChargeUserSingleSelectionRef.showUserPlusModal([formData.inChargeUserId])"
						/>
					</a-form-item>
					<a-form-item label="负责人ID：" name="inChargeUserId" hidden>
						<a-input v-model:value.trim="formData.inChargeUserId" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="安全员ID：" name="safetyUserId" hidden>
						<a-input v-model:value.trim="formData.safetyUserId" />
					</a-form-item>
					<a-form-item label="安全员：" name="safetyUserName">
						<a-input
							v-model:value="formData.safetyUserName"
							placeholder="请输入安全员"
							allow-clear
							@click="safetyUserSingleSelectionRef.showUserPlusModal([formData.safetyUserId])"
						/>
					</a-form-item>
				</a-col>
			</a-row>

			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="类型：" name="category">
						<a-select v-model:value="formData.category" placeholder="请选择类型" :options="categoryOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="性质：" name="natures">
						<a-select v-model:value="formData.natures" placeholder="请选择性质" :options="naturesOptions" />
					</a-form-item>
				</a-col>
			</a-row>
			<a-form-item label="描述：" name="remark">
				<a-textarea v-model:value="formData.remark" placeholder="请输入描述" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
	<user-selector-plus
		ref="inChargeUserSingleSelectionRef"
		page-url="/sys/role/userSelector"
		org-url="/sys/role/orgTreeSelector"
		:radio-model="true"
		@onBack="afterSelectInChargeUser"
	/>
	<user-selector-plus
		ref="safetyUserSingleSelectionRef"
		page-url="/sys/role/userSelector"
		org-url="/sys/role/orgTreeSelector"
		:radio-model="true"
		@onBack="afterSelectSafetyUser"
	/>
</template>

<script setup name="bizLocationRoomForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizLocationRoomApi from '@/api/biz/bizLocationRoomApi'
	import bizLocationAreaApi from '@/api/biz/bizLocationAreaApi'
	import bizLocationBuildingApi from '@/api/biz/bizLocationBuildingApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import userSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const inChargeUserSingleSelectionRef = ref({})
	const safetyUserSingleSelectionRef = ref({})
	const submitLoading = ref(false)
	const categoryOptions = ref([])
	const naturesOptions = ref([])
	const areaOptions = ref([])
	const buildingOptions = ref([])
	const treeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		categoryOptions.value = tool.dictList('ROOM_TYPE')
		naturesOptions.value = tool.dictList('ROOM_NATURE')
		bizLocationAreaApi.bizLocationAreaList({}).then((res) => {
			areaOptions.value = res.map((item) => {
				return {
					label: `${item.code}-${item.name}`,
					value: item.id
				}
			})
		})
		bizLocationBuildingApi.bizLocationBuildingList({}).then((res) => {
			buildingOptions.value = res.map((item) => {
				return {
					label: `${item.code}-${item.name}`,
					value: item.id
				}
			})
		})
		// 获取机构树并加入顶级
		bizOrgApi.orgTree().then((res) => {
			treeData.value = [
				{
					id: 0,
					parentId: '-1',
					name: '顶级',
					children: res
				}
			]
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		code: [required('请输入门牌号')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			bizLocationRoomApi
				.bizLocationRoomSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	const afterSelectInChargeUser = (value) => {
		if (value.length > 0) {
			formData.value.inChargeUserId = value[0].id
			formData.value.inChargeUserName = value[0].name
		} else {
			formData.value.inChargeUserId = null
			formData.value.inChargeUserName = null
		}
	}

	const afterSelectSafetyUser = (value) => {
		if (value.length > 0) {
			formData.value.safetyUserId = value[0].id
			formData.value.safetyUserName = value[0].name
		} else {
			formData.value.safetyUserId = null
			formData.value.safetyUserName = null
		}
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
